home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / jabber / filetransfer / initiateStream.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  15KB  |  375 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from jabber.filetransfer.s5b_proxy_sender import S5B_proxyConnect
  5. from jabber.objects.bytestreams import ByteStreams, BYTESTREAMS_NS
  6. from jabber.objects.si_filetransfer import Feature
  7. from jabber.objects.si_filetransfer import SI_FileTransfer, SI_NS
  8. from jabber.objects.si_filetransfer import File
  9. from jabber.filetransfer.S5BFileXferHandler import ByteStream
  10. import functools
  11. import random
  12. from jabber.filetransfer import supported_streams
  13. from pyxmpp.all import Iq
  14. from util.net import FileChunker
  15. from logging import getLogger
  16. log = getLogger('jabber.file.send')
  17. from common import pref
  18. from common.filetransfer import OutgoingFileTransfer, FileTransfer
  19. from path import path
  20. from util import lock
  21. done_states = FileTransfer.states.CompleteStates | FileTransfer.states.FailStates
  22.  
  23. def if_not_done(f):
  24.     
  25.     def wrapper1(self, *args, **kw):
  26.         if self.state not in done_states:
  27.             return f(self, *args, **kw)
  28.         
  29.  
  30.     wrapper1 = (functools.wraps(f),)(wrapper1)
  31.     return wrapper1
  32.  
  33.  
  34. class SIsender(OutgoingFileTransfer):
  35.     
  36.     def __init__(self, jabber_protocol, jid, filestorage, message = None):
  37.         OutgoingFileTransfer.__init__(self)
  38.         self.j = jabber_protocol
  39.         self.jid = jid
  40.         self.filestor = filestorage
  41.         self.message = message
  42.         self.sid = 'si_' + str(id(self)) + str(random.randint(0, 100))
  43.         log.info('SIsender created for %r', jid)
  44.         self.completed = 0
  45.         self.size = filestorage.size
  46.         self.filepath = path(self.filestor.path)
  47.         self.buddy = self.j.buddies[jid]
  48.         self.name = self.filestor.name
  49.         self.on_get_buddy(self.buddy)
  50.  
  51.     
  52.     def protocol(self):
  53.         return self.j
  54.  
  55.     protocol = property(protocol)
  56.     
  57.     def send_offer(self):
  58.         self.state = self.states.WAITING_FOR_BUDDY
  59.         i = Iq(to_jid = self.jid, stanza_type = 'set')
  60.         si = SI_FileTransfer(self.sid)
  61.         si.file = File(self.filestor.name, self.filestor.size)
  62.         si.feature = Feature(possible_streams = supported_streams)
  63.         si.as_xml(i.get_node())
  64.         self.j.send_cb(i, success = self.handle_response, error = self.handle_error, timeout = self.timed_out)
  65.  
  66.     
  67.     def handle_response(self, stanza):
  68.         self.state = self.states.CONNECTING
  69.         si = SI_FileTransfer.from_iq(stanza)
  70.         self.stream = b = stream_connectors[si.feature.selected_stream](self)
  71.         b.bind_event('stream_connected', self.transferring)
  72.         b.bind_event('stream_connect_failed', self.stream_connect_failed)
  73.         b.bind_event('stream_error', self.stream_error)
  74.         b.bind_event('stream_closed', self.stream_closed)
  75.         b.connect_stream()
  76.  
  77.     handle_response = lock(if_not_done(handle_response))
  78.     
  79.     def stream_connect_failed(self):
  80.         self.unbind_all()
  81.         self.state = self.states.CONN_FAIL
  82.         self.on_error()
  83.  
  84.     stream_connect_failed = lock(if_not_done(stream_connect_failed))
  85.     
  86.     def handle_error(self, stanza):
  87.         e = stanza.get_error()
  88.         SI_NS_error = e.get_condition(SI_NS)
  89.         if SI_NS_error is not None:
  90.             SI_NS_error = SI_NS_error.name
  91.         
  92.         error = e.get_condition()
  93.         if error is not None:
  94.             error = error.name
  95.         
  96.         if SI_NS_error == 'no-valid-streams':
  97.             self.state = self.states.CONN_FAIL
  98.         elif SI_NS_error == 'bad-profile':
  99.             self.state = self.states.CONN_FAIL
  100.         elif error == 'forbidden':
  101.             reason = e.get_text()
  102.             log.info('%r: handle_error. Setting state to CANCELLED_BY_BUDDY')
  103.             self.state = self.states.CANCELLED_BY_BUDDY
  104.         else:
  105.             self.state = self.states.CONN_FAIL
  106.         if self.state == self.states.CONN_FAIL:
  107.             self.on_error()
  108.         
  109.  
  110.     handle_error = lock(if_not_done(handle_error))
  111.     
  112.     def timed_out(self):
  113.         log.info('%r: Timed out. Setting state to CONN_FAIL, calling on_error')
  114.         self.unbind_all()
  115.         self.state = self.states.CONN_FAIL
  116.         self.on_error()
  117.  
  118.     timed_out = lock(if_not_done(timed_out))
  119.     
  120.     def stream_error(self):
  121.         log.info('%r: Stream error. Setting state to CONN_FAIL_XFER, calling on_error')
  122.         self.unbind_all()
  123.         self.close_file()
  124.         self.state = self.states.CONN_FAIL_XFER
  125.         self.on_error()
  126.  
  127.     stream_error = lock(if_not_done(stream_error))
  128.     
  129.     def stream_closed(self):
  130.         log.info('%r: Stream closed. Setting state to CANCELLED_BY_BUDDY')
  131.         self.unbind_all()
  132.         self.close_file()
  133.         self.state = self.states.CANCELLED_BY_BUDDY
  134.  
  135.     stream_closed = lock(if_not_done(stream_closed))
  136.     
  137.     def unbind_all(self):
  138.         if hasattr(self, 'stream'):
  139.             b = self.stream
  140.             b.unbind('stream_connected', self.transferring)
  141.             b.unbind('stream_connect_failed', self.stream_connect_failed)
  142.             b.unbind('stream_error', self.stream_error)
  143.             b.unbind('stream_closed', self.stream_closed)
  144.         
  145.  
  146.     unbind_all = lock(unbind_all)
  147.     
  148.     def transferring(self):
  149.         self.stream.unbind('stream_connected', self.transferring)
  150.         self.state = self.states.TRANSFERRING
  151.         self.chunker = FileChunker(self.filestor.obj, close_when_done = True, progress_cb = self.on_progress)
  152.         self.stream.conn.push_with_producer(self.chunker)
  153.         self.stream.conn.close_when_done()
  154.  
  155.     transferring = lock(if_not_done(transferring))
  156.     
  157.     def cancel(self, state = None):
  158.         self.unbind_all()
  159.         if hasattr(self, 'stream'):
  160.             self.stream.cancel()
  161.             if hasattr(self, 'chunker'):
  162.                 self.chunker.cancelled = True
  163.             
  164.         
  165.         self.close_file()
  166.         if state is None:
  167.             state = self.states.CANCELLED_BY_YOU
  168.         
  169.         self.state = state
  170.  
  171.     cancel = lock(if_not_done(cancel))
  172.     
  173.     def on_progress(self, bytes):
  174.         self._setcompleted(bytes)
  175.         if self.completed == self.size:
  176.             self.unbind_all()
  177.             self.stream.close()
  178.             self.state = self.states.FINISHED
  179.             self._ondone()
  180.         
  181.  
  182.     on_progress = lock(if_not_done(on_progress))
  183.     
  184.     def close_file(self):
  185.         
  186.         try:
  187.             self.chunker.fileobj.close()
  188.         except Exception:
  189.             pass
  190.  
  191.         
  192.         try:
  193.             self.filestor.obj.close()
  194.         except Exception:
  195.             pass
  196.  
  197.  
  198.  
  199.  
  200. class SOCKS5OutBytestream(ByteStream):
  201.     
  202.     def __init__(self, sender):
  203.         ByteStream.__init__(self)
  204.         self.j = sender.j
  205.         self.jid = sender.jid
  206.         self.sid = sender.sid
  207.         self.conn = None
  208.  
  209.     
  210.     def connect_stream(self):
  211.         
  212.         try:
  213.             self.hash = self.j.s5bserver.conn_id(self.sid, self.j.stream.me, self.jid)
  214.         except AttributeError:
  215.             self.event('stream_connect_failed')
  216.             return None
  217.  
  218.         self.j.s5bserver.add_hash(self.hash)
  219.         i = Iq(to_jid = self.jid, stanza_type = 'set')
  220.         b = ByteStreams(sid = self.sid)
  221.         if pref('jabber.use_proxy_ft'):
  222.             b.hosts.extend(set((lambda .0: for hosts in .0:
  223. for h in hosts:
  224. h)(self.j.known_s5b_proxies.values())))
  225.             if pref('jabber.use_jabber_org_proxy', True):
  226.                 b.add_host('proxy.jabber.org', '208.245.212.98', 7777)
  227.             
  228.         
  229.         b.as_xml(i.get_node())
  230.         self.j.send_cb(i, success = self.handle_ready, error = self.handle_error, timeout = self.timed_out)
  231.  
  232.     
  233.     def kill_socket_hash(self):
  234.         c = self.j.s5bserver.retrieve_hash(self.hash)
  235.         if c not in (False, None):
  236.             
  237.             try:
  238.                 c.close()
  239.  
  240.         
  241.  
  242.     
  243.     def timed_out(self):
  244.         self.kill_socket_hash()
  245.         self.event('stream_connect_failed')
  246.  
  247.     
  248.     def handle_error(self, stanza):
  249.         import traceback as traceback
  250.         traceback.print_exc()
  251.         self.kill_socket_hash()
  252.         self.event('stream_connect_failed')
  253.         log.info(stanza.serialize())
  254.  
  255.     
  256.     def handle_ready(self, stanza):
  257.         log.info(stanza.serialize())
  258.         
  259.         try:
  260.             b = ByteStreams(stanza.get_query())
  261.             used_jid = b.host_used
  262.         except:
  263.             self.kill_socket_hash()
  264.             self.event('stream_connect_failed')
  265.             return None
  266.  
  267.         if not pref('jabber.use_proxy_ft'):
  268.             pass
  269.         
  270.         if not pref('jabber.use_direct_ft'):
  271.             pass
  272.         
  273.         log.info('handle_ready done')
  274.  
  275.     
  276.     def socket_failed(self):
  277.         self.unbind_all()
  278.         self.event('stream_connect_failed')
  279.  
  280.     
  281.     def socket_connected(self):
  282.         self.conn.unbind('connection_failed', self.socket_failed)
  283.         self.conn.unbind('connected', self.handle_proxy_connect)
  284.         self.conn.bind_event('socket_error', self.stream_error)
  285.         self.conn.bind_event('socket_closed', self.stream_closed)
  286.  
  287.     
  288.     def stream_closed(self):
  289.         self.unbind_all()
  290.         self.event('stream_closed')
  291.  
  292.     
  293.     def stream_error(self):
  294.         self.unbind_all()
  295.         self.event('stream_error')
  296.  
  297.     
  298.     def unbind_all(self):
  299.         self.conn.unbind('connection_failed', self.socket_failed)
  300.         self.conn.unbind('connected', self.handle_proxy_connect)
  301.         self.conn.unbind('socket_error', self.stream_error)
  302.         self.conn.unbind('socket_closed', self.stream_closed)
  303.  
  304.     
  305.     def handle_proxy_failure(self):
  306.         log.info('handle proxy failure')
  307.         self.unbind_all()
  308.         self.event('stream_connect_failed')
  309.  
  310.     
  311.     def handle_proxy_failure2(self):
  312.         log.info('handle proxy failure2')
  313.         
  314.         try:
  315.             self.conn.close()
  316.         except:
  317.             import traceback
  318.             traceback.print_exc()
  319.  
  320.         self.event('stream_connect_failed')
  321.  
  322.     
  323.     def handle_proxy_connect(self):
  324.         log.info('handle_proxy_connect called')
  325.         self.conn.set_terminator(0)
  326.         streamhost = self.streamhost
  327.         sh_jid = streamhost.jid
  328.         targ_jid = self.jid
  329.         b = ByteStreams(None, self.sid)
  330.         b.activate = targ_jid
  331.         i = Iq(to_jid = sh_jid, stanza_type = 'set')
  332.         b.as_xml(i.get_node())
  333.         self.j.send_cb(i, success = self.handle_proxy_activate, error = self.handle_proxy_failure2, timeout = self.proxy_activate_timeout)
  334.  
  335.     
  336.     def proxy_activate_timeout(self):
  337.         self.unbind_all()
  338.         
  339.         try:
  340.             self.conn.close()
  341.         except:
  342.             import traceback
  343.             traceback.print_exc()
  344.  
  345.         self.event('stream_connect_failed')
  346.  
  347.     
  348.     def handle_proxy_activate(self, stanza):
  349.         log.info('handle_proxy_activate called')
  350.         self.socket_connected()
  351.         self.event('stream_connected')
  352.  
  353.     
  354.     def close(self):
  355.         self.conn.close_when_done()
  356.  
  357.     
  358.     def cancel(self):
  359.         
  360.         try:
  361.             self.conn.close()
  362.         except:
  363.             pass
  364.  
  365.  
  366.  
  367.  
  368. def dumpfile(conn, filestor, progress_cb):
  369.     log.info('dumpfile called')
  370.     conn.push_with_producer()
  371.     conn.close_when_done()
  372.  
  373. stream_connectors = {
  374.     BYTESTREAMS_NS: SOCKS5OutBytestream }
  375.